<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\cache\driver\Redis;
use think\Db;
use think\Request;
use function fast\e;


/**
 * 店铺接口
 */
class Shop extends Api
{
    protected $noNeedLogin = ['list','detail','category','goodslist','goodsdetail'];
//    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';
//    protected $url = 'https://online5g.bigchun.com/api';
    protected $url =  '';
    protected $dshtoken = '';
    protected function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->user_id = $this->auth->id;
        $this->dshtoken = session('dshtoken');
        $redis = new Redis();
        $this->dshtoken = $redis->get('dshtoken_'.$this->user_id);
        $this->url =  config('dsh_yuming');
    }

    protected function checkuser(){
        $useritem = Db::name('share_user')->where('user_id',$this->user_id )->find();
        if($useritem['role']!=3){
            $this->error(__('不满足开店等级，联系管理员'));
        }
    }

   /* //入驻店铺
    public function entry(Request $request){
        self::checkuser();
        //一星网店主可以开店
        $params = $this->request->post("");
        $url = $this->url . '/api/shop/entry';
        $params['domain'] = $request->domain();
        $data = postCurl($url, $params,$this->dshtoken);
        $data? $this->success('ok',$data) : $this->error(__('服务器繁忙'));
    }

    public function complete(Request $request){
        self::checkuser();
        $params = $this->request->post("");
        $url = $this->url . '/api/shop/complete';
        $params['domain'] = $request->domain();
        $data = postCurl($url, $params,$this->dshtoken);
        $data? $this->success('ok',$data) : $this->error(__('服务器繁忙'));
    }*/

    //入驻店铺
    public function entry(Request $request){
        $params = $this->request->post("");
        $url = $this->url . '/api/shop/entry';
        $params['domain'] = $request->domain();
        $data = postCurl($url, $params);
        $data? $this->success('ok',$data) : $this->error(__('服务器繁忙'));
    }

    public function complete(Request $request){
        $params = $this->request->post("");
        $url = $this->url . '/api/shop/complete';
        $params['domain'] = $request->domain();
        $data = postCurl($url, $params);
        $data? $this->success('ok',$data) : $this->error(__('服务器繁忙'));
    }

    //所有店铺列表
    public function list()
    {
        $params = $this->request->post("");
        $url = $this->url . '/api/shop/list';
        $data = postCurl($url, $params);
        if($data['data']['code']==1){
            $returndata = $data['data']['data'];
            $this->success('ok',$returndata);
        }else{
            $this->error('多商户返回异常');
        }

    }


    /*//店铺认证详情
    public function authdetail(Request $request){
        $params = $this->request->post("");
        $url = $this->url . '/api/shop/authdetail';
        $params['domain'] = $request->domain();
        $data = postCurl($url, $params,$this->dshtoken);
        $this->success('ok',$data);
    }*/

    //店铺认证详情
    public function authdetail(Request $request){
        $params = $this->request->post("");
        $url = $this->url . '/api/shop/authdetail';
        $params['domain'] = $request->domain();
        $data = postCurl($url, $params);
        $this->success('ok',$data);
    }


    //店铺详情
    public function detail(){
        $params = $this->request->post("");
        $url = $this->url . '/api/shop/detail';
        $data = postCurl($url, $params);
        $this->success('ok',$data);
    }

    //分类列表
    public function category(){
        $params = $this->request->post("");
        $url = $this->url . '/api/shop/category';
        $data = postCurl($url, $params);
        $this->success('ok',$data);
    }

    //商品列表
    public function goodslist(){
        $params = $this->request->post("");
        $url = $this->url . '/api/shop/goodslist';
        $data = postCurl($url, $params);
        $this->success('ok',$data);
    }

    //商品详情
    public function goodsdetail(){
        $params = $this->request->post("");
        $id = $params['id'];
        $url = $this->url . '/api/shop/goodsdetail';
        $data = postCurl($url, $params);
        $this->success('ok',$data);
    }

    //定时同步分润信息
    public function fenrun(){
        $url = $this->url . '/api/Futurethree/orderSynchronization';
        $list = Db::name('shopro_order')->where(array('is_dsh'=>1,'is_tb'=>0))
            ->filed('id,order_sn,goods_amount as money,consignee as addr_name,phone as addr_mobile,concat(province_name,city_name,city_name) as address,user_id,')
            ->limit(20)->select();
        $params = [];
        $ids = [];
        if(!empty($list)){
            foreach ($list as $key=>$val){
                $list[$key]['money_log'] = Db::name('shopro_order')->where(array('is_dsh'=>1,'is_tb'=>0))->select();
                if($val['type']==5){
                    $memo = '进货成本';
                }elseif($val['type']==6){
                    $memo = '一星网点佣金';
                }else{
                    $memo = '其他';
                }
                $params[$key] = [
                    'order_id' => $val['order_id'],
                    'money' => $val['money'],
                    'memo' => $memo,
                    'shop_id'=>$val['shop_id']
                ];
                array_push($ids,$val['id']);
            }
            //查询未同步的分润
            $data = postCurl($url, $params);
            Db::name('share_money')->where('id','in',$ids)->update(array('is_tb'=>1));
        }
        $this->success('ok');
    }


}
